<?php
load_info_var($cf_controller);	
load_file('power_group_class',LIB_PATH);
$power_group = new PowerGroup($db);

$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$pid = isset($_POST['pid']) ? intval($_POST['pid']) : intval($_GET['pid']);
switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller,$cf_action,0) ;
		//load_file('page_class',LIB_PATH);
		//$MyPage = new Mypage($db);
		//$data_ary = $MyPage->from_sql('*','sys_group',"1","g_id DESC",intval($_GET['cpage']),20);
		//$page = $MyPage->get_page($data,$cf_self_path.'-act_index.htm');

        $data_ary = $power_group->get_group_array();
		//my_print($data_ary);
		if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

                }
                //my_print($data_ary);
	break;
	case "add":
		if($cf_ispost){
			$g_id = trim($_POST['g_id']);
			$g_title = trim($_POST['g_title']);
			$g_dsc = trim($_POST['g_dsc']);
			$g_type = intval($_POST['g_type']);
			$g_club_type = trim($_POST['g_club_type']);
			$g_groups = is_array($_POST['g_groups']) ? implode(",",$_POST['g_groups']) : '';
			$g_act_power = is_array($_POST['powers']) ? implode(",",$_POST['powers']) : '';
			$data = array(	
				'g_pid'=>$pid,
				'g_title'=>$g_title,
				'g_dsc'=>$g_dsc,
				'g_type'=>$g_type,
				'g_club_type'=>$g_club_type,
				'g_act_power'=>$g_act_power,
				'g_groups'=>$g_groups
			);
			
			//my_print($data);
			//$db->begin();
			$power_group = new PowerGroup($db);			
			$result = $power_group->insert_group($data,$pid,$g_id);
			if($result){
				my_jump('添加成功','',$cf_self_page.'?dpt='.$cf_dpt,2);
			}else{
				my_jump('添加失败');
			}
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			
			
			$prow = $power_group->get_group_row($pid);

			$row['g_groups'] = $prow['g_groups'] ? explode(",",$prow['g_groups']) : '';
			$row['powers'] = $prow['g_act_power'] ? explode(",",$prow['g_act_power']) : '';
			
			$powerlist = $power_group->get_power_acl();
			$grouplist = $power_group->get_group_list();

                        
		}
    break;
	
	case "edit":
		if($cf_ispost){
			$g_title = trim($_POST['g_title']);
			$g_dsc = trim($_POST['g_dsc']);
			//$g_view_power = $_POST['g_view_power'];
			$g_type = intval($_POST['g_type']);
			$g_club_type = trim($_POST['g_club_type']);
			$g_groups = is_array($_POST['g_groups']) ? implode(",",$_POST['g_groups']) : '';
			$g_act_power = is_array($_POST['powers']) ? implode(",",$_POST['powers']) : '';
			$data = array(
				'g_id'=>$id,				
				'g_pid'=>$pid,	
				'g_title'=>$g_title,
				'g_dsc'=>$g_dsc,
				'g_type'=>$g_type,
				'g_club_type'=>$g_club_type,
				'g_act_power'=>$g_act_power,
				'g_groups'=>$g_groups
			);
			//$db->setDebug(1,1);
			$result = $power_group->update_group($data,"g_id='".$id."'");
			if($result)
			my_jump('修改成功','',$cf_self_page.'?dpt='.$cf_dpt,2);
			else
			my_jump('修改失败');
		
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$row = $power_group->get_group_row($id);
			/*if($cf_admin_info['admin_gid']!='1'){
				my_jump('You do not have permission');
			}else{
				$row['powers'] = explode(",",$row['g_act_power']);
			}*/
			$row['powers'] = explode(",",$row['g_act_power']);
			$powerlist = $power_group->get_power_acl();
			$grouplist = $power_group->get_group_list();
			$row['g_more_power'] = $row['g_more_power'] ? explode(",",$row['g_more_power']) : '';
			
			
			
		}
    break;
	case "viewedit":
		if($cf_ispost){
			$idlist = $_POST['idlist'];
			if(!$idlist)
			my_jump('选项不能为空');
			
			$g_view_power = implode(",",$idlist);
			$data = array(	
				'g_view_power'=>$g_view_power,
			);
			$result = $power_group->update_group($data,"g_id='".$id."'");
			if($result){
				load_file('menu_class',LIB_PATH);
				//$menu = new Menu($db);
				$menuob->update_data_file($id);
				my_jump('修改成功','',$cf_self_page.'?dpt='.$cf_dpt,2);
			}
			else{
				my_jump('修改失败');
			}
		
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$row = $power_group->get_group_row($id);			
			//if($row['g_act_power']=='all'){
				//my_jump('You do not have permission');
			//}else{
				$row['powers'] = explode(",",$row['g_view_power']);
			//}
			$powerlist = $power_group->get_power_acl();
			
			
			//加载菜单
			load_file('menu_class',LIB_PATH);
			$menu = new Menu($db);
			$menu_array = $menu->load_data();
			//my_print($menu_array);
			foreach($menu_array as $k=>$v){
				$menu_array[$k]['checkbox'] = in_array($v['menu_id'],$row['powers']) ? "checked='checked'" : "";
				if(isset($v['child'])){
					foreach($v['child'] as $k2=>$v2){
						$menu_array[$k]['child'][$k2]['checkbox'] = in_array($v2['menu_id'],$row['powers']) ? "checked='checked'" : "";
						
						if(isset($v2['child'])){
							foreach($v2['child'] as $k3=>$v3){
								$menu_array[$k]['child'][$k2]['child'][$k3]['checkbox'] = in_array($v3['menu_id'],$row['powers']) ? "checked='checked'" : "";
							}
						}
					}
				}
			}
		
		}
    break;
	case "copy":
	    if($cf_ispost){
			$listid=trim($_POST['listid']);
			$gid=trim($_POST['gid']);
			$copy_data = $power_group->get_group_row($gid);
			$data = array(	
				'g_act_power'=>$copy_data['g_act_power'],
				'g_view_power'=>$copy_data['g_view_power'],
			);
			
			$result = $power_group->update_group($data,"g_id in (". $listid . ")");
			if($result)
				my_jump('復制成功','操作提示',"javascript:my_close();");
			else
				my_jump('復制失败');
		
		}else{
			load_file('admin_class', LIB_PATH);
			$main = new Admin($db);
			$group = $main->get_group_ary(3);
			$smarty->mypage("group_info",$cf_action,0) ;
			$listid=$_GET['idlist'];
			$sql="select * from `sys_group` where g_id in (". $listid . ") order by g_id ASC";
			$data_ary = $db->getAll($sql);
			if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

                }
			
			}
	
	break;
	
	//複製瀏覽权限
	case "copyviewpower":
	    if($cf_ispost){
			$listid=trim($_POST['listid']);
			$gid=trim($_POST['gid']);
			$copy_data = $power_group->get_group_row($gid);
			$data = array(				
				'g_view_power'=>$copy_data['g_view_power'],
			);
			
			$result = $power_group->update_group($data,"g_id in (". $listid . ")");
			if($result)
				my_jump('復制瀏覽权限成功','操作提示',"javascript:my_close();");
			else
				my_jump('復制瀏覽权限失败');
		
		}else{
			load_file('admin_class', LIB_PATH);
			$main = new Admin($db);
			$group = $main->get_group_ary(3);
			$smarty->mypage("group_info",$cf_action,0) ;
			$listid=$_GET['idlist'];
			$sql="select * from `sys_group` where g_id in (". $listid . ") order by g_id ASC";
			$data_ary = $db->getAll($sql);
			if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

                }
			
			}
	
	break;
	
	//添加瀏覽权限
	case "addviewpower":
	 if($cf_ispost){
		    load_file('admin_class', LIB_PATH);
			$main = new Admin($db);
			$menu = new Menu($db);			
			$power_id=trim($_POST['power_id']);
			$power_id_arr=explode(",",$power_id);
			$arr_count=count($power_id_arr);	//输入浏览权限的个数
			$menu_array = $menu->get_data_array('menu_id in('.$power_id.')');	//获得相应的浏览菜单				
		    $select_count=count($menu_array);	//根据输入的浏览权限查询到的浏览权限			
			
			if($arr_count != $select_count){
				my_jump('瀏覽权限不存在！');
			}
		
		    $gid_str=trim($_POST['listid']);
			if(is_array($power_id_arr)){			
				foreach($power_id_arr as $key=>$val){				   
				  $result = $power_group->update_view_power($val,$gid_str,$power_id);
				   if($result){
					   $tip.=$val."添加瀏覽权限成功<br>";
					 }else{
					   $tip.=$val."添加瀏覽权限失败<br>";
					}			   
				}
			
			}			
			echo $tip;
			exit;
			
		}else{
			load_file('admin_class', LIB_PATH);
			$main = new Admin($db);		
			$group = $main->get_group_ary(3);
			$smarty->mypage("group_info",$cf_action,0) ;
			$listid=$_GET['idlist'];
			$sql="select * from `sys_group` where g_id in (". $listid . ") order by g_id ASC";
			$data_ary = $db->getAll($sql);
			if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

               }
			
		}
	break;
	
	
	case "del":
		$result = $power_group->del_group($id);
		if($result)
			my_jump('删除成功','',$cf_self_page.'?dpt='.$cf_dpt,2);
		else
			my_jump('删除失败');
	break;
	case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "alldel":
					$result = $power_group->del_group("g_id in (".$list.") and g_act_power!='all'");
				break;
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
			my_jump('更新完成','',$cf_self_page.'?dpt='.$cf_dpt,2);
			else 
			my_jump('更新失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	case "addpower":
	 if($cf_ispost){
		    load_file('admin_class', LIB_PATH);
			$main = new Admin($db);		
			$power_id=trim($_POST['power_id']);
			$power_id_arr=explode(",",$power_id);
			$arr_count=count($power_id_arr);
			$data = $power_group->get_power_array('power_id in('.$power_id.')','power_id DESC','0','power_id');			
		    $select_count=count($data);
			
			if($arr_count != $select_count){
				my_jump('权限不存在！');
			}
		
		    $gid_str=trim($_POST['listid']);
			if(is_array($power_id_arr)){			
				foreach($power_id_arr as $key=>$val){				   
				  $result = $power_group->update_act_power($val,$gid_str,$power_id);
				   if($result){
					   $tip.=$val."添加权限成功<br>";
					 }else{
					   $tip.=$val."添加权限失败<br>";
					}			   
				}
			
			}
			
			echo $tip;
			exit;
			
		}else{
			load_file('admin_class', LIB_PATH);
			$main = new Admin($db);		
			$group = $main->get_group_ary(3);
			$smarty->mypage("group_info",$cf_action,0) ;
			$listid=$_GET['idlist'];
			$sql="select * from `sys_group` where g_id in (". $listid . ") order by g_id ASC";
			$data_ary = $db->getAll($sql);
			if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

               }
			
		}
	break;
	case "delpower":
	 if($cf_ispost){
		   load_file('admin_class', LIB_PATH);
			$main = new Admin($db);		
			$power_id=trim($_POST['power_id']);
			$power_id_arr=explode(",",$power_id);
			$arr_count=count($power_id_arr);
			$data = $power_group->get_power_array('power_id in('.$power_id.')','power_id DESC','0','power_id');			
		    $select_count=count($data);
			
			if($arr_count != $select_count){
				my_jump('所选組別权限不存在！');
			}
		   
			$gid=explode(',',trim($_POST['listid']));
			if(is_array($power_id_arr)){	
				foreach($power_id_arr as $key=>$val){
					
						foreach($gid as $key2=>$val2){
						 $result = $power_group->del_act_power($val,$val2);
						 if($result){
						  $tip.="权限ID".$val."在gid=".$val2."删除权限成功<br>";
						  $data_str= $power_group->get_group_row($val2,'g_act_power');
						  $g_act_power= trim($data_str['g_act_power'],',');
						  $data=array('g_act_power'=>$g_act_power);
						  $power_group->update_group($data,"g_id = '".$val2."'");
						   
						 }else{
						   $tip.="权限ID".$val."在gid=".$val2."删除权限失败<br>";
						}	
						}
							   
				 }
			
			}
			echo $tip;
			exit;
			
		}else{
			load_file('admin_class', LIB_PATH);
			$main = new Admin($db);		
			$group = $main->get_group_ary(3);
			$smarty->mypage("group_info",$cf_action,0) ;
			$listid=$_GET['idlist'];
			$sql="select * from `sys_group` where g_id in (". $listid . ") order by g_id ASC";
			$data_ary = $db->getAll($sql);
			if($data_ary){
                    foreach ($data_ary as $k => $v) {
                        if($v['g_level']){
                            $data_ary[$k]['indent'] = str_repeat("&nbsp;",$v['g_level']*3).'├';
                        }
                    }

               }
			
		}
	break;
	
	
	default:
		my_jump('非法操作!');
	break;
}

$smarty->assign(array(
	'id'=>$id,
	'rid'=>$rid,
	'row'=>htmlchar($row),
	'powerlist'=>$powerlist,
	'grouplist'=>$grouplist,
	'data_ary'=>$data_ary,
	'menu_array'=>$menu_array,
	'page'=>$page,
	'group'=>$group,
	'listid'=>$listid,
	'group_arr'=>$group_arr,

));
$smarty->mydisplay();

?>